using Xml;

namespace Atlantis
{
    public class ProjectWriter : GLib.Object
    {
		string fileName;
		Xml.Ns* ns;
		
		public ProjectWriter(string fileName){
			this.fileName = fileName;
		}
		
		public void write_project(ProjectNode node){
			Xml.Doc* xmldoc = new Xml.Doc("1.0");
			//ns = new Xml.Ns(null, null, "");
			//ns->type = Xml.ElementType.ELEMENT_NODE;
			Xml.Node* currentNode = new Xml.Node(null,"hidalgo-project");
			xmldoc->set_root_element(currentNode);
			currentNode->new_prop("fileversion", "1.0" );


			save_project( node, currentNode);
			
			xmldoc->save_file(this.fileName);
		}
		
		private void save_project(ProjectNode node,  Xml.Node* parentNode){
			Xml.Node* projectXmlNode = parentNode->new_text_child(ns,"project","");
			
			init_default_properties(node, projectXmlNode);
			
			projectXmlNode->new_prop("name", node.Text );
			
			foreach( Node childNode in node.Children ) {
				if (childNode is DocumentNode) {
					save_document((DocumentNode)childNode, projectXmlNode);
				}
			}
		}
		
		private void save_document(DocumentNode node,  Xml.Node* parentNode){
			Xml.Node* documentNode = parentNode->new_text_child(ns,"document","");
			
			init_default_properties(node, documentNode);
			
			documentNode->new_prop("name", node.Text );
			documentNode->new_text_child(ns,"path", node.RelativePath);
			
			documentNode->new_prop("name", node.Text );
			documentNode->new_text_child(ns,"path", node.RelativePath);
		}
		
		private void init_default_properties(Node node,  Xml.Node* xmlNode){
			xmlNode->new_prop("id", node.Id );
		}
    }
}
